import java.util.Scanner;

/**
 * @author gao
 * @date 2025/7/18 14:25
 */

public class Solution {
    public int minCost(int[][] costs) {
        // 获取costs数组的长度
        int n = costs.length;
        // 创建一个二维数组dp，用于存储每个位置的最小花费
        int[][] dp = new int[n + 1][3];
        // 遍历costs数组
        for (int i = 1; i <= n; i++) {
            // 计算当前位置的最小花费
            dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + costs[i - 1][0];
            dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + costs[i - 1][1];
            dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1]) + costs[i - 1][2];
        }
        // 返回最后一个位置的最小花费
        return Math.min(dp[n][0], Math.min(dp[n][1], dp[n][2]));
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int m = in.nextInt();
            int n = in.nextInt();
            int[][] costs = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    costs[i][j] = in.nextInt();
                }
            }
            Solution solution = new Solution();
            System.out.println(solution.minCost(costs));
        }
    }
}